package com.fluidops.datacatalog.reltables.util;

import java.util.Map;

import com.wcohen.ss.ScaledLevenstein;
import com.wcohen.ss.api.StringDistance;
import com.wcohen.ss.api.StringWrapper;

public class StringCompare {

	public static final int CACHE_SIZE = 500;  
	
	private StringDistance stringDistance = new ScaledLevenstein();
	
	private Map<String, StringWrapper> cache;
	
	public StringCompare() {
		// behaves like a LRU-Cache, with enabled accessOrdering
		this.cache = new LRUCache<String, StringWrapper>(CACHE_SIZE);
	}
	
	public double compareStrings(String a, String b){
		double similarity = 0d;

		if(a != null && b != null){
			StringWrapper swa;
			StringWrapper swb;

			if(!this.cache.containsKey(a)){
				this.cache.put(a, this.stringDistance.prepare(a));
			}
			swa = this.cache.get(a);

			if(!this.cache.containsKey(b)){
				this.cache.put(b, this.stringDistance.prepare(b));
			}
			swb = this.cache.get(b);

			similarity = this.stringDistance.score(swa, swb);
		}
		return similarity;
	}
}
